package com.stu.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.stu.core.dao.UserDao;
import com.stu.entity.mysite_enity.User;

public class UserDaoImpl implements UserDao{

	public void save(Connection conn, User user) throws SQLException {
		PreparedStatement ps = conn.prepareCall("INSERT INTO users(name, password, email) VALUES (?,?,?)");

		ps.setString(1, user.getName());
		ps.setString(2, user.getPasswd());
		ps.setString(3, user.getEmail());
		ps.execute();

	}

	public void update(Connection conn, long id, User user) throws SQLException {
		String updateSql = "UPDATE users SET name = ?, password = ?, email = ? WHERE id = ?";
		PreparedStatement ps = conn.prepareStatement(updateSql);

		ps.setString(1, user.getName());
		ps.setString(2, user.getPasswd());
		ps.setString(3, user.getEmail());
		ps.setLong(4, id);
		ps.execute();
	}

	public void delete(Connection conn, User user) throws SQLException {
		PreparedStatement ps = conn.prepareStatement("DELETE FROM users WHERE id = ?");
		ps.setLong(1, user.getId());
		ps.execute();
	}


	public ResultSet get(Connection conn, User user) throws SQLException {
		PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE name = ? AND password = ?");
		ps.setString(1, user.getName());
		ps.setString(2, user.getPasswd());
		ResultSet rs = ps.executeQuery();
//		while(rs.next()){
//			 for(int i=1;i<=3;i++){
//			     System.out.print(rs.getString(i));
//			     System.out.print("\t\t");
//		    }
//		    System.out.println();
//		}
		return rs;
	}



	

}
